package net.maku.member.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.member.convert.MemberTagConvert;
import net.maku.member.entity.MemberTagEntity;
import net.maku.member.service.MemberTagService;
import net.maku.member.query.MemberTagQuery;
import net.maku.member.vo.MemberTagVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
* 会员标签
*
* @author 阿沐 babamu@126.com
* @since 1.0.0 2025-03-16
*/
@RestController
@RequestMapping("member/tag")
@Tag(name="会员标签")
@AllArgsConstructor
public class MemberTagController {
    private final MemberTagService memberTagService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('member:tag:page')")
    public Result<PageResult<MemberTagVO>> page(@ParameterObject @Valid MemberTagQuery query){
        PageResult<MemberTagVO> page = memberTagService.page(query);

        return Result.ok(page);
    }

    @GetMapping("list")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('member:tag:list')")
    public Result<List<MemberTagVO>> list(){
        return Result.ok(memberTagService.getList());
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('member:tag:info')")
    public Result<MemberTagVO> get(@PathVariable("id") Long id){
        MemberTagEntity entity = memberTagService.getById(id);

        return Result.ok(MemberTagConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('member:tag:save')")
    public Result<String> save(@RequestBody MemberTagVO vo){
        memberTagService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('member:tag:update')")
    public Result<String> update(@RequestBody @Valid MemberTagVO vo){
        memberTagService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('member:tag:delete')")
    public Result<String> delete(@RequestBody List<Long> idList){
        memberTagService.delete(idList);

        return Result.ok();
    }
}